package com.easy.order.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.common.bean.Payment;
import com.easy.common.service.IPaymentService;
import com.easy.order.mapper.PaymentMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@DubboService
public class PaymentServiceImpl extends ServiceImpl<PaymentMapper, Payment> implements IPaymentService {

    @Override
    public Payment getPaymentById(int id) {
        return super.getOne(new QueryWrapper<Payment>().eq("id", id));
    }

    @Override
    public List<Payment> getPaymentsByUserId(int userId) {
        LambdaQueryWrapper<Payment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Payment::getUser_id, userId);
        queryWrapper.eq(Payment::getIs_del, 0);
        return super.list(queryWrapper);
    }

    @Override
    public List<Payment> getPaymentsBySeckillGoodsId(int seckillGoodsId) {
        LambdaQueryWrapper<Payment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Payment::getSeckill_goods_id, seckillGoodsId);
        queryWrapper.eq(Payment::getIs_del, 0);
        return super.list(queryWrapper);
    }

    @Override
    public List<Payment> getPaymentsByGoodsId(int goodsId) {
        LambdaQueryWrapper<Payment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Payment::getGoods_id, goodsId);
        queryWrapper.eq(Payment::getIs_del, 0);
        return super.list(queryWrapper);
    }

    @Override
    public int addPayment(Payment payment) {
        // 确保删除状态默认为0(未删除)
        if (payment.getIs_del() == null) {
            payment.setIs_del(0);
        }
        return super.save(payment) ? 1 : 0;
    }

    @Override
    public int updatePayment(Payment payment) {
        return super.updateById(payment) ? 1 : 0;
    }

    @Override
    public int deletePayment(Integer id) {
        // 软删除，将is_del字段置为1
        return super.update().set("is_del", 1).eq("id", id).update() ? 1 : 0;
    }

    @Override
    public List<Payment> getAll(Payment payment) {
        LambdaQueryWrapper<Payment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Payment::getIs_del, 0);
        return super.list(queryWrapper);
    }

    @Override
    public IPage<Payment> getPage(Payment payment, IPage<Payment> page) {
        LambdaQueryWrapper<Payment> queryWrapper = new LambdaQueryWrapper<>();
        // 只有当user_id不为空时才添加查询条件
        if (payment.getUser_id() != null && !payment.getUser_id().toString().isEmpty()) {
            queryWrapper.like(Payment::getUser_id, payment.getUser_id());
        }
        // 只有当goods_id不为空时才添加查询条件
        if (payment.getGoods_id() != null && !payment.getGoods_id().toString().isEmpty()) {
            queryWrapper.like(Payment::getGoods_id, payment.getGoods_id());
        }
        // 只有当seckill_goods_id不为空时才添加查询条件
        if (payment.getSeckill_goods_id() != null && !payment.getSeckill_goods_id().toString().isEmpty()) {
            queryWrapper.like(Payment::getSeckill_goods_id, payment.getSeckill_goods_id());
        }
        queryWrapper.eq(Payment::getIs_del, 0);
        return super.page(page, queryWrapper);
    }
}
